package cn.edu.live.controller;

import cn.edu.live.dao.UserDao;
import cn.edu.live.interceptor.CheckIfLoginInterceptor;
import cn.edu.live.model.UserInfo;
import cn.edu.live.status.RespStatus;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

public class UserController extends Controller {

	/**
	 * 注册页面
	 */
	public void registPage() {
		renderJsp("/regist.jsp");
	}

	/**
	 * 登录页面
	 */
	public void loginPage() {
		renderJsp("/login.jsp");
	}

	/**
	 * 个人主页，检查是否登陆
	 */
	@Before(CheckIfLoginInterceptor.class)
	public void myPage() {
		renderJsp("/myPage.jsp");
	}

	/**
	 * 用户注册
	 */
	public void register() {

		int code = -1;
		String msg = "注册失败，请稍后重试！";

		// 获取表单传入的参数
		String username = getPara("username");
		String password = getPara("password");
		String usermail = getPara("usermail");
		int type = getParaToInt("usertype");

		UserInfo userInfo = new UserDao().findUesrInfo(0, username, null);

		if (userInfo == null) {

			boolean success = new UserDao().saveUser(username, password, usermail, type);
			if (success) {
				code = RespStatus.SUCCESS;
				msg = "注册成功！";

				redirect("/user/loginPage");
				return;
			}
		} else {
			msg = "该用户名已被注册!";
		}

		setAttr("respcontent", msg);
		renderJsp("/regist.jsp");
	}

	/**
	 * 用户登录
	 */
	public void login() {

		// 获取表单传入的参数
		String username = getPara("username");
		String password = getPara("password");
		int type = getParaToInt("type");

		int code = -1;
		String msg = "登陆失败！请稍后重试！";// 存放登录信息

		UserInfo userInfo = new UserDao().findUesrInfo(0, username, null);

		if (userInfo != null) {

			if (userInfo.getStr("password").equals(password) && userInfo.getInt("type") == type) {
				// 成功登录，进入主页
				code = RespStatus.SUCCESS;
				msg = "登录成功!";// 登录成功

				// 将登录的用户名和用户类型以及用户信息是否完善记录到session
				getSession().setAttribute("username", username);
				getSession().setAttribute("infoid", userInfo.getInt("id"));
				// getSession().setAttribute("isComplete",
				// userInfo.get("isComplete"));
				getSession().setAttribute("type", type);

				redirect("/activity/getAllLiveAct");
				return;
			} else {
				// 密码输入错误
				msg = "用户名或密码错误！";// 密码输入错误，清空密码框
			}
		} else {
			// 登录失败，提示用户不存在
			msg = "用户不存在";// 输入的用户名不存在，清空输入输入框
		}

		setAttr("respcontent", msg);
		renderJsp("/login.jsp");
	}

	/**
	 * 注销
	 */
	public void logout() {

		if (getSessionAttr("username") != null) {
			removeSessionAttr("username");
		}
		if (getSessionAttr("infoid") != null) {
			removeSessionAttr("infoid");
		}

		redirect("/user/loginPage");
	}

	/**
	 * 用户信息完善
	 */
	public void completeInfo() {
		getSession().setAttribute("isCompleted", "true");// TODO 如果只进了这个页面却没完善？
		String isCompleted = "true";
		String username = getSessionAttr("username");
		String type = getSessionAttr("type");

		if (type.equals("anchor")) {// 用户类型是主播

			// 获取从表单传入的参数
			int gender = Integer.parseInt(getPara("gender"));
			String headIcon = getPara("headIcon");
			String phoneNumber = getPara("phoneNumber");
			String qqNumber = getPara("qqNumber");
			String school = getPara("school");
			String major = getPara("major");
			String grade = getPara("grade");
			String address = getPara("address");
			String description = getPara("description");// 小于100�?

			Db.update("update userinfo set isCompleted = ? where username=? and type=?", isCompleted, username, type);
			Db.update(
					"update ? set gender=?,headIcon=?,phoneNumber=?,qqNumber=?,school=?,major=?,grade=?,address=?,description=?"
							+ "where username=?", type, gender, headIcon, phoneNumber, qqNumber, school, major, grade,
					address, description);

		}

		else {// 用户类型是学生

			// 获取从表单传入的参数
			int gender = Integer.parseInt(getPara("gender"));
			String headIcon = getPara("headIcon");
			String school = getPara("school");
			String grade = getPara("grade");
			String address = getPara("address");
			String description = getPara("description");// 小于100�?

			Db.update("update userinfo set isCompleted = ? where username=?,type=?", isCompleted, username, type);
			Db.update("update ? set gender=?,headIcon=?,school=?,grade=?,address=?,description=?" + "where username=?",
					type, gender, headIcon, school, grade, address, description);
		}
	}

	/**
	 * 拼接返回json对象用
	 * 
	 * @param code
	 * @param errMsg
	 * @return
	 */
	private JSONObject respJson(int code, String msg) {
		JSONObject js = new JSONObject();
		js.put("code", code);
		js.put("msg", msg);
		return js;
	}

	private JSONObject respJson(int code, String msg, JSONObject data) {
		JSONObject js = new JSONObject();
		js.put("code", code);
		js.put("msg", msg);
		js.put("data", data);
		return js;
	}

}